package view.component.window
{
import flash.geom.Point;

import framework.view.components.container.IRenderProxy;

import mapengine.interfaces.IFactoryBuilding;

import utils.TimeUtil;

import values.product.ProductValue;

import view.component.render.FactoryProductItemRender;

/**
 * FactoryProductUIProxy
 * @author jliang
 *
 */
public class FactoryProductUIProxy extends PaginationWindowProxy
{
	//==========================================================================
	//  Constructor
	//==========================================================================
	public function FactoryProductUIProxy()
	{
		super();
	}
	//==========================================================================
	//  Properties
	//==========================================================================
	protected function get products():Vector.<ProductValue>
	{
		return data.factoryBuildingItemValue.productList;
	}
	
	protected var _data:IFactoryBuilding;
	
	public function get data():IFactoryBuilding
	{
		return _data;
	}
	
	public function set data(value:IFactoryBuilding):void
	{
		_data = value;
		
		skin.titleLabel.text = data.buildingItemValue.template.name;
	}
	//==========================================================================
	//  Overridden methods: ComponentProxy
	//==========================================================================
	override protected function get repeaterItemRenderClass():Class
	{
		return FactoryProductItemRender;
	}
	
	override protected function clickToGoPrevHandler():void
	{
		updateList(products, products.length, currentPage - 1);
	}
	
	override protected function clickToGoNextHandler():void
	{
		updateList(products, products.length, currentPage + 1);
	}
	//==========================================================================
	//  Public methods
	//==========================================================================
	override public function show(lock:Boolean=true, position:Point=null, ...args):void
	{
		super.show.apply(this, [ lock, position ].concat(args)); 
		
		initData();
	}
	
	override public function dispose():void
	{
		super.dispose();
	}
	//==========================================================================
	//  Protected methods
	//==========================================================================
	protected function initData():void
	{			
		var pageCount:int = Math.ceil(products.length / preCount);
		
		var page:int = currentPage;
		if (pageCount - 1 < currentPage)
		{
			page = pageCount - 1;
			if (page < 0)
			{
				page = 0;
			}
		}
		updateList(products, products.length, page);
	}
	//==========================================================================
	//  Event handlers
	//==========================================================================
	override protected function listItemSelectedHandler(render:IRenderProxy):void
	{
		var pvo:ProductValue = render.data;		
		data.produce(pvo.key, TimeUtil.currentGameTimeSec);		
		close();
	}
}
}




